// const baseUrl = 'http://wxamp100.natapp1.cc'
// // const baseUrl = 'https://new.mkxd.cn/wxapp'

import {
	baseUrl
} from '@/common/config.js'

//封装post请求
function post(url, data) {
	console.log(baseUrl+url)
	return new Promise((resolve, reject) => {
		let token = uni.getStorageSync('token')
		uni.getNetworkType({
			success: res => {
				if (res.networkType == 'none') {
					uni.showToast({
						title: '无网络连接，请检查网络后再试！',
						icon: 'none'
					})
				} else {
					if (token != '') {
						uni.request({
							url: baseUrl + url,
							method: 'POST',
							data,
							header: {
								Authorization: 'Bearer' + ' ' + uni.getStorageSync('token'),
								'content-type': 'application/json;charset=UTF-8',
								'clientId': 'be7052a7e4f802c20df10a8d131adb12'
							},
							success: res => {
								if (res.data.code == 200) {
									resolve(res)
								} else if (res.data.code == 401) {} else {
									resolve(res)
								}
							},
							fail: res => {}
						})
					} else {
						uni.request({
							url: baseUrl + url,
							method: 'POST',
							header: {
								'content-type': 'application/json;charset=UTF-8',
								'clientId': 'be7052a7e4f802c20df10a8d131adb12'
							},
							data,
							success: res => {
								if (res.data.code == 200) {
									resolve(res)
								} else if (res.data.code == 401) {} else {
									resolve(res)
								}
							},
							fail: res => {}
						})
					}
				}
			},
			fail: res => {
				uni.showToast({
					title: '无法获取网络状态，请检查网络！',
					icon: 'none'
				})
			}
		})
	})
}


//封装get请求
function get(url, data) {
	console.log(baseUrl+url)
	return new Promise((resolve, reject) => {
		uni.getNetworkType({
			success: res => {
				if (res.networkType == 'none') {
					uni.showToast({
						title: '无网络连接，请检查网络后再试！',
						icon: 'none'
					})
				} else {
					let header = {}
					if (uni.getStorageSync('token')) {
						header = {
							Authorization: 'Bearer' + ' ' + uni.getStorageSync('token'),
							'content-type': 'application/json;charset=UTF-8',
							'clientId': 'be7052a7e4f802c20df10a8d131adb12'
						}
					} else {
						header = {
							'content-type': 'application/json;charset=UTF-8',
							'clientId': 'be7052a7e4f802c20df10a8d131adb12'
						}
					}
					uni.request({
						url: baseUrl + url,
						method: 'GET',
						data,
						header: header,
						success: res => {
							// uni.hideLoading()
							if (res.data.code == 200) {
								resolve(res)
							} else if (res.data.code == 401) {} else {
								resolve(res)
							}
						},
						fail: res => {}
					})
				}
			},
			fail: res => {
				uni.showToast({
					title: '无法获取网络状态，请检查网络！',
					icon: 'none'
				})
			}
		})
	})
}


//封装put请求
function put(url, data) {
	return new Promise((resolve, reject) => {
		uni.getNetworkType({
			success: res => {
				if (res.networkType == 'none') {
					uni.showToast({
						title: '无网络连接，请检查网络后再试！',
						icon: 'none'
					})
				} else {
					let header = {}
					if (uni.getStorageSync('token')) {
						header = {
							Authorization: 'Bearer' + ' ' + uni.getStorageSync('token'),
							'content-type': 'application/json;charset=UTF-8',
							'clientId': 'be7052a7e4f802c20df10a8d131adb12'
						}
					} else {
						header = {
							'content-type': 'application/json;charset=UTF-8',
							'clientId': 'be7052a7e4f802c20df10a8d131adb12'
						}
					}
					uni.request({
						url: baseUrl + url,
						method: 'PUT',
						data,
						header: header,
						success: res => {
							// uni.hideLoading()
							if (res.data.code == 200) {
								resolve(res)
							} else if (res.data.code == 401) {} else {
								resolve(res)
							}
						},
						fail: res => {}
					})
				}
			},
			fail: res => {
				uni.showToast({
					title: '无法获取网络状态，请检查网络！',
					icon: 'none'
				})
			}
		})
	})
}


//封装delet请求
function del(url, data) {
	return new Promise((resolve, reject) => {
		uni.getNetworkType({
			success: res => {
				if (res.networkType == 'none') {
					uni.showToast({
						title: '无网络连接，请检查网络后再试！',
						icon: 'none'
					})
				} else {
					let header = {}
					if (uni.getStorageSync('token')) {
						header = {
							Authorization: 'Bearer' + ' ' + uni.getStorageSync('token'),
							'content-type': 'application/json;charset=UTF-8',
							'clientId': 'be7052a7e4f802c20df10a8d131adb12'
						}
					} else {
						header = {
							'content-type': 'application/json;charset=UTF-8',
							'clientId': 'be7052a7e4f802c20df10a8d131adb12'
						}
					}
					uni.request({
						url: baseUrl + url,
						method: 'DELETE',
						data,
						header: header,
						success: res => {
							// uni.hideLoading()
							if (res.data.code == 200) {
								resolve(res)
							} else if (res.data.code == 401) {} else {
								resolve(res)
							}
						},
						fail: res => {}
					})
				}
			},
			fail: res => {
				uni.showToast({
					title: '无法获取网络状态，请检查网络！',
					icon: 'none'
				})
			}
		})
	})
}

function uploadImg(data) {
	return new Promise((resolve, reject) => {
		uni.getNetworkType({
			success: res => {
				if (res.networkType == 'none') {
					uni.showToast({
						title: '无网络连接，请检查网络后再试！',
						icon: 'none'
					})
				} else {
					uni.uploadFile({
						url: baseUrl + '/xcx/oss/upload',
						filePath: data,
						fileType: 'image',
						name: 'file',
						header: {
							Authorization: 'Bearer' + ' ' + uni.getStorageSync('token'),
							// 'content-type': 'multipart/form-data',
							'clientId': 'be7052a7e4f802c20df10a8d131adb12'
						},
						success: res => {
							res.data = JSON.parse(res.data)
							if (res.data.code == 200) {
								resolve(res.data)
							} else {
								resolve(res)
							}
						},
						fail: res => {
							console.log(res)
							uni.showModal({
								title: '上传失败',
								content: JSON.stringify(res)
							})
						}
					})
				}
			},
			fail: res => {
				uni.showToast({
					title: '无法获取网络状态，请检查网络！',
					icon: 'none'
				})
			}
		})
	})
}

function uploadVideo(data) {
	return new Promise((resolve, reject) => {
		let token = uni.getStorageSync('token')
		uni.getNetworkType({
			success: res => {
				if (res.networkType == 'none') {
					uni.showToast({
						title: '无网络连接，请检查网络后再试！',
						icon: 'none'
					})
				} else {
					uni.uploadFile({
						url: baseUrl + 'user/uploadvideo',
						filePath: data,
						fileType: 'video',
						name: 'file',
						token: token,
						success: res => {
							res.data = JSON.parse(res.data)
							if (res.data.code == 200) {
								resolve(res.data)
							} else {
								resolve(res)
							}
						},
						fail: res => {
							console.log(res)
							uni.showToast({
								title: '上传失败',
								icon: 'none'
							})
						}
					})
				}
			},
			fail: res => {
				uni.showToast({
					title: '无法获取网络状态，请检查网络！',
					icon: 'none'
				})
			}
		})
	})
}
export default {
	post,
	get,
	put,
	del,
	uploadImg,
	uploadVideo
}